package com.lagou.service.impl;

import com.lagou.common.Const;
import com.lagou.common.ServerResponse;
import com.lagou.mapper.IndustryMapper;
import com.lagou.service.IIndustryService;
import com.lagou.vo.ConverterVo;
import com.lagou.vo.IndustryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndustryServiceImpl implements IIndustryService {

    @Autowired
    private IndustryMapper mapper;

    /**
     * 统计各个城市招聘情况
     * @return
     */
    @Override
    public ServerResponse<Map<String, Object>> countCitysData() {

        //查询各个城市的招聘情况，创建一个对象封装查询数据

         List<ConverterVo> cityPositidonList=mapper.countCityPositidon();


         Map<String,Object> map = new HashMap<>();

        //创建3个list集合，cityNameList存放城市名称 thirdList 前三名城市数据 dataList 存放各个城市数据
        List<String> cityNamesList = new ArrayList<>();
        List<ConverterVo> thirdList = new ArrayList<>();
        List<ConverterVo> dataList = new ArrayList<>();

        //循环list，给cityNameList thirdList dataList 封装数据

        for (ConverterVo converterVo : cityPositidonList) {
            cityNamesList.add(converterVo.getName());

            if (thirdList.size() < 3){
                thirdList.add(converterVo);
            }

            dataList.add(converterVo);
        }

        map.put("cityNamesList",cityNamesList);
        map.put("thirdList",thirdList);
        map.put("dataList",dataList);

        //把3个list存放到map中

        //把map中的数据存到serverResponse中返回

        return ServerResponse.createBySuccessData(map);
    }

    @Override
    public ServerResponse<Map<String, Object>> getIndustryTop() {

        //查询最热门的10个行业，List<IndustryVo>
        List<IndustryVo> converterVoList = mapper.countIndustryTop();

        //创建map 封装数据
        Map<String,Object> map = new HashMap<>();

        List<String> industryNames = new ArrayList<>();
        List<Long> countList = new ArrayList<>();

        for (IndustryVo industryVo : converterVoList) {

            industryNames.add(industryVo.getPositionName());
            countList.add(industryVo.getNum());
        }


        map.put("names",industryNames);
        map.put("count",countList);


        return ServerResponse.createBySuccessData(map);
    }


    /**
     * 统计热门行业 在2020-6-1 --2020/06/09的销售情况
     * @return
     */
    @Override
    public ServerResponse<Map<String, Object>> getIndustryCompare() {


        //创建map集合，封装最终数据
        Map<String,Object> map = new HashMap<>();

        //循环行业
        for (String industryName : Const.INDUSTRY) {

            List<Long> countNumList = new ArrayList<>();
            //创建List<> 记录行业每天招聘情况
           List<IndustryVo> dataList= mapper.getIndustryDate(industryName,Const.START_DATE,Const.END_DATE);

           //dataList,目的给countNumList设置数组

            for (IndustryVo industryVo : dataList) {
              countNumList.add(industryVo.getNum());
            }
            //判断行业。给map中设置值
            if (industryName.equals(Const.INDUSTRY[0])){
                map.put("sell",countNumList);
            }else if(industryName.equals(Const.INDUSTRY[1])){
                map.put("server",countNumList);
            }else if(industryName.equals(Const.INDUSTRY[2])){
                map.put("pro",countNumList);
            }
        }

        map.put("dateTime",Const.DATE_TIME);
        map.put("industryNameList",Const.INDUSTRY);

        return ServerResponse.createBySuccessData(map);
    }

    @Override
    public ServerResponse<Map<String, Object>> getIndustryTop10() {

        //查询平均工资最高的是个城市
        List<IndustryVo> converterVoList = mapper.countIndustryTop10();

        //创建map 封装数据
        Map<String,Object> map = new HashMap<>();

        List<BigDecimal> avgSalarys = new ArrayList<>();
        List<String> cityList = new ArrayList<>();

        for (IndustryVo industryVo : converterVoList) {

            avgSalarys.add(industryVo.getAvgSalary());
            cityList.add(industryVo.getCity());
        }


        map.put("avgSalary",avgSalarys);
        map.put("cityList",cityList);

        return ServerResponse.createBySuccessData(map);
    }
}
